package com.cennavi.minenavidemo.utils;

import android.text.TextUtils;

import java.math.BigDecimal;

public class FormatUtil {

    /**
     * 时间的统一表示方式
     *
     * @param mm 毫秒
     */
    public static String FormatTime(long mm) {
        int hour, minute, second;
        second = (int) (mm / 1000);
        hour = second / 3600;
        minute = (second - hour * 3600) / 60;

        if (hour == 0) {
            return minute + " 分钟";
        }

        return hour + " 小时 " + minute + " 分钟";
    }

    /**
     * 时间的统一表示方式
     *
     * @param mm 毫秒
     */
    public static String FormatTime2(long mm) {
        int hour, minute, second;
        second = (int) (mm / 1000);
        hour = second / 3600;
        minute = (second - hour * 3600) / 60;

        if (hour == 0) {
            return minute + "分钟";
        }
        return hour + "小时 " + minute + "分钟";
    }

    /**
     * 时间的统一表示方式
     *
     * @param mm 毫秒
     */
    public static String FormatTime3(long mm) {
        int hour, minute, second;
        second = (int) (mm / 1000);
        hour = second / 3600;
        minute = (second - hour * 3600) / 60;

        if (hour == 0) {
            return minute + "分钟";
        }
        return hour + "小时" + minute + "分钟";
    }

    /**
     * 时间的统一表示方式
     *
     * @param mm 毫秒
     * @return
     */
    public static String FormatTime00(long mm) {
        int hour, minute, second;
        second = (int) (mm / 1000);
        hour = second / 3600;
        minute = (second - hour * 3600) / 60;

        if (minute < 10) {
            return hour + ":0" + minute;
        }
        return hour + ":" + minute;
    }

    /**
     * 时间的表示方式
     *
     * @param mm 毫秒
     * @return XX分钟
     */
    public static int FormatTimeToMinute(long mm) {
        int minute, second;
        second = (int) (mm / 1000);
        minute = second / 60;
        return minute;
    }

    /**
     * 距离的KM表示方式
     */
    public static String FormatDistByKM(long ulDist) {
        String strdist;

        /* 避免显示出1.0公里、2.0公里、9.0公里等 */
        if (ulDist / 100 * 100 % 1000 == 0) {
            strdist = ulDist / 1000 + "";
        } else {
            /* sprintf函数会自动四舍五入*/
            float fDistanceResult = (float) ulDist / (float) 1000;
            BigDecimal org = new BigDecimal(fDistanceResult);
            org = org.setScale(1, BigDecimal.ROUND_HALF_UP);
            fDistanceResult = org.floatValue();

            if (fDistanceResult * 10 % 10 == 0) {
                strdist = (int) fDistanceResult + "";
            } else {
                strdist = fDistanceResult + "";
            }
        }

        return strdist;
    }

    public static String FormatDistKm(long ulDist) {
        String strdist = FormatDist(ulDist);
        strdist = strdist.replace("米", "m");
        strdist = strdist.replace("公里", "Km");
        return strdist;
    }

    /**
     * 距离的公里表示方式
     */
    public static String FormatDistOnKM(long ulDist) {
        String strdist;
        int ulDistanceResult;
        if (ulDist < 10) {
            strdist = "0.1";
        } else if (ulDist < 1000) {
            ulDistanceResult = Math.round((float) ulDist / 10) * 10;

            if (ulDistanceResult == 1000) {
                strdist = "1";
            } else {
                strdist = "0." + ulDistanceResult / 100;
            }
        } else if (ulDist < 100000) {
            /* 避免显示出1.0公里、2.0公里、9.0公里等 */
            if (ulDist / 100 * 100 % 1000 == 0) {
                strdist = ulDist / 1000 + "";
            } else {
                // ulDist = (ulDist ) /100 * 100;
                /* sprintf函数会自动四舍五入*/
                float fDistanceResult = (float) ulDist / (float) 1000;
                BigDecimal org = new BigDecimal(fDistanceResult);
                org = org.setScale(1, BigDecimal.ROUND_HALF_UP);
                fDistanceResult = org.floatValue();
                if (fDistanceResult * 10 % 10 == 0) {
                    strdist = (int) fDistanceResult + "";
                } else {
                    strdist = fDistanceResult + "";
                }
            }
        } else {
            ulDistanceResult = Math.round((float) ulDist / 1000);
            if (ulDistanceResult > 9999) {
                ulDistanceResult = 9999;
            }
            strdist = ulDistanceResult + "";
        }
        return strdist;
    }

    /**
     * 距离的统一表示方式
     */
    public static String FormatDist(long ulDist) {
        String strdist;
        int ulDistanceResult;

        if (ulDist == 0)
            ulDist = 1;

        if (ulDist < 10) {
            strdist = ulDist + "米";
        } else if (ulDist < 1000) {
            ulDistanceResult = Math.round((float) ulDist / 10) * 10;
            if (ulDistanceResult == 1000) {
                strdist = "1公里";
            } else {
                strdist = ulDistanceResult + "米";
            }
        } else if (ulDist < 100000) {
            strdist = inOneAndHundredKM(ulDist);
        } else {
            strdist = moreHundredKM(ulDist);
        }

        return strdist;
    }

    /**
     * 大于1公里小于100公里的距离格式化
     */
    public static String inOneAndHundredKM(long ulDist) {
        String strdist;
        /* 避免显示出1.0公里、2.0公里、9.0公里等 */
        if (ulDist / 100 * 100 % 1000 == 0) {
            strdist = ulDist / 1000 + "公里";
        } else {
            // ulDist = (ulDist ) /100 * 100;
            /* sprintf函数会自动四舍五入*/
            float fDistanceResult = (float) ulDist / (float) 1000;
            BigDecimal org = new BigDecimal(fDistanceResult);
            org = org.setScale(1, BigDecimal.ROUND_HALF_UP);
            fDistanceResult = org.floatValue();

            if (fDistanceResult * 10 % 10 == 0) {
                strdist = (int) fDistanceResult + "公里";
            } else {
                strdist = fDistanceResult + "公里";
            }
        }

        return strdist;
    }

    /**
     * 大于100公里的距离格式化
     */
    public static String moreHundredKM(long ulDist) {
        String strdist;
        int ulDistanceResult = Math.round((float) ulDist / 1000);

        if (ulDistanceResult > 9999) {
            ulDistanceResult = 9999;
        }

        strdist = ulDistanceResult + "公里";
        return strdist;
    }

    /**
     * 格式化周期Str
     *
     * @param week
     */
    public static String FormatWeek(String week) {
        String pushDates = "";

        if (!TextUtils.isEmpty(week)) {
            if (week.equals("1|2|3|4|5")) {
                pushDates = "工作日";
            } else if (week.equals("6|7")) {
                pushDates = "周末";
            } else if (week.equals("1|2|3|4|5|6|7")) {
                pushDates = "每天";
            } else {
                String str = week;
                str = str.replace("1", "一");
                str = str.replace("2", "二");
                str = str.replace("3", "三");
                str = str.replace("4", "四");
                str = str.replace("5", "五");
                str = str.replace("6", "六");
                str = str.replace("7", "日");
                str = str.replace("|", ",");
                pushDates = "周" + str;
            }
        }

        return pushDates;
    }
}
